#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
ll fastpow(ll b, ll p, ll mod) {
  ll res = 1 % mod;
  while (p) {
    if (p & 1) res = res * b % mod;
    b = b * b % mod;
    p >>= 1;
  }
  return res;
}
ll phi(ll n) {
  ll ans = n;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i) continue;
    while (n % i == 0) n /= i;
    ans = ans / i * (i - 1);
  }
  if (n > 1) ans = ans / n * (n - 1);
  return ans;
}
void solve() {
  cin >> n;
  if (n % 2 == 0) n /= 2;
  if (n % 2 == 0 || n % 5 == 0) {
    cout << -1 << endl;
    return;
  }
  n *= 9;
  ll x = phi(n);
  set<ll> a;
  for (ll i = 1; i * i <= x; i++) {
    if (x % i) continue;
    a.insert(i);
    if (i * i != x) a.insert(x / i);
  }
  for (ll x : a) {
    if (fastpow(10, x, n) == 1) {
      cout << x << endl;
      return;
    }
  }
  cout << -1 << endl;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}